import React, { Component } from 'react';
import { Card, Table } from 'antd';
import { getSchedule } from 'Services/publicServer.js';
import get from 'lodash/get';

const WEEK_MAP = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Satur', 'Sun'];

class publicServerNotice extends Component {
  constructor(props) {
    super(props);
    window.scope = this;
  }

  state = {
    dataSource: [],
  };

  componentDidMount() {
    this.fetchData();
  }

  fetchData = async () => {
    const res = await getSchedule({ id: '1' });
    const dataSource = [];
    const data = get(res, 'data', []);
    const num = Math.max(...data.map(({ lineNum }) => +lineNum + 1));

    for (let i = 0; i < num; i += 1) {
      const obj = {
        [WEEK_MAP[0]]: null,
        [WEEK_MAP[1]]: null,
        [WEEK_MAP[2]]: null,
        [WEEK_MAP[3]]: null,
        [WEEK_MAP[4]]: null,
        [WEEK_MAP[5]]: null,
        [WEEK_MAP[6]]: null,
      };
      dataSource.push(obj);
    }

    data.forEach(({ whatDay, lineNum }) => {
      dataSource[lineNum][WEEK_MAP[whatDay - 1]] = '✓';
    });
    this.setState({ dataSource });
  };

  columns = [
    {
      title: '班次',
      key: '班次',
      render: (_, __, index) => <span>第{index + 1}班</span>,
    },
    {
      title: '星期一',
      key: '星期一',
      dataIndex: WEEK_MAP[0],
    },
    {
      title: '星期二',
      key: '星期二',
      dataIndex: WEEK_MAP[1],
    },
    {
      title: '星期三',
      key: '星期三',
      dataIndex: WEEK_MAP[2],
    },
    {
      title: '星期四',
      key: '星期四',
      dataIndex: WEEK_MAP[3],
    },
    {
      title: '星期五',
      key: '星期五',
      dataIndex: WEEK_MAP[4],
    },
    {
      title: '星期六',
      key: '星期六',
      dataIndex: WEEK_MAP[5],
    },
    {
      title: '星期日',
      key: '星期日',
      dataIndex: WEEK_MAP[6],
    },
  ];

  render() {
    const { columns } = this;
    const { dataSource } = this.state;

    return (
      <div>
        <Card title="排班信息">
          <Table columns={columns} dataSource={dataSource} pagination={false} bordered />
        </Card>
      </div>
    );
  }
}

export default publicServerNotice;
